package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbee.smartcommunities.entity.Category;
import com.fbee.smartcommunities.entity.Permission;
import com.fbee.smartcommunities.entity.UploadFile;
import com.fbee.smartcommunities.mapper.CategoryMapper;
import com.fbee.smartcommunities.mapper.PermissionMapper;
import com.fbee.smartcommunities.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.UploadFileService;
import com.fbee.smartcommunities.utils.CustomizeException;
import com.mysql.cj.util.StringUtils;
import io.minio.errors.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-18
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private UploadFileService uploadFileService;

    @Override
    public List<Category> getlist() {
        return getParentCategory(0);
    }

    /**
     * 递归将每个SysResource的子节点都封装进去
     * @param parentId
     * @return
     */
    private List<Category> getParentCategory(Integer parentId){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<Category> list = categoryMapper.selectList(queryWrapper);
        for (Category category : list) {
            // 获得孩子节点，给父节点的属性进行赋值
            List<Category> parentCategory = getParentCategory(category.getId());
            category.setChildren(parentCategory);
        }
        return list;
    }

    @Override
    public Boolean update(Long id, String name, Integer type,MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String icon = null;
        UpdateWrapper<Category> categoryWrapper = new UpdateWrapper<>();
        if(file != null){
            icon = uploadFileService.upload("icon",file);
            categoryWrapper.set("icon",icon);
        }
        categoryWrapper.eq("id",id);
        categoryWrapper.set("name",name);
        categoryWrapper.set("type",type);
        return this.update(categoryWrapper);
    }

    @Override
    public Boolean check(String filedName, String value, Long id) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(filedName,value);
        if(id != null) {
            // 说明这时候是修改时候的校验
            queryWrapper.ne("id",id);
        }
        return count(queryWrapper) > 0;
    }

    @Override
    public Boolean delete(Long id) throws CustomizeException {
        // 判断是否有下级，如何判断 将这id作为父id查询 看是否能够查询出来
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        if(!this.list(queryWrapper).isEmpty()){
            throw new CustomizeException("请先删除下级权限");
        }
        this.removeById(id);
        return true;
    }

    @Override
    public Boolean addCategory(String name, Long parentId, Integer level, Integer type, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String icon = null;
        if(file != null){
            icon = uploadFileService.upload("icon",file);
        }
        Category category = new Category(name, parentId, type, level, icon);
        return save(category);
    }


}
